package com.qf.controller;

import com.qf.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/json")
@RestController//如果 该类上有这个注解 意味着该类所有方法的返回值均为JSON格式字符串
public class JsonController {

    @RequestMapping("/test1")
    @ResponseBody// 将该方法的返回值自动转换为JSON 格式字符串 并响应给客户端
    public User test1() {
        User user = new User(1, "fan", 3500.5, new Date());
        return user;
    }

    @RequestMapping("/test2")
    @ResponseBody
    public List<User> test2() {
        List<User> users = new ArrayList<>();
        users.add(new User(1, "fan", 3500.5, new Date()));
        users.add(new User(2, "fanff", 3500.5, new Date()));
        return users;
    }

    //如果返回值如果是个String  那么不在转换JSON 直接以字符串返回
    @RequestMapping(value = "/test3", produces = "text/html;charset=utf-8  ")
//    @ResponseBody
    public String test3() {
//        return "OK";
        return "山西";
    }


    @RequestMapping("/test4")
    public String test(@RequestBody User user) {
        System.out.println(user);
        return "thanks";
    }


}
